package main

import (
	"fmt"
	"io"
	"net/http"
)

func handler(res http.ResponseWriter, req *http.Request) {
	fmt.Println(req.Method)
	switch req.Method {
	case http.MethodGet:

		// res.WriteHeader(400)
		header := res.Header()
		header["test"] = []string{"ddd", "aaa"}
		res.Header().Add("ddd", "aaaa")
		res.Write([]byte("get 我收到了给你返回"))
		break
	case http.MethodPost:
		res.WriteHeader(400)
		b, _ := io.ReadAll(req.Body)
		//json.Unmarshal(b,&user) 解析成结构体
		b = []byte(string(b) + "88888 我收到了给你返回")
		header := req.Header
		fmt.Println(header["De"], header["de"])
		//header["test"] = []string{"ddd", "ffffffff"}
		res.Header().Add("name", ":dddd")
		//res.Write([]byte("post 我收到了给你返回"))
		//req.Header.Set("name", "123")
		res.Write(b)
	}
}

func main() {
	fmt.Println("server")

	mux := http.NewServeMux() // mux
	mux.HandleFunc("/test", handler)
	//http.HandleFunc("/test", handler)
	// 监听
	//http.ListenAndServe(":8080", nil)
	http.ListenAndServe(":8080", mux)
}
